package com.example.rpc.framework.protocl.http;

import com.alibaba.fastjson.JSONObject;
import com.example.rpc.framework.Invocation;

import java.net.URI;
import java.net.http.HttpRequest;//jdk 11中自带HttpClient
import java.net.http.HttpResponse;

public class HttpClient {
    public String send(String hostName, Integer port, Invocation invocation){
        try{
            HttpRequest request= HttpRequest.newBuilder()
                    .uri(new URI("http",null,hostName,port,"","",null))
                    .POST(HttpRequest.BodyPublishers.ofString(JSONObject.toJSONString(invocation)))
                    .build();
            java.net.http.HttpClient client=java.net.http.HttpClient.newHttpClient();
            HttpResponse<String> response=client.send(request, HttpResponse.BodyHandlers.ofString());
            String result=response.body();
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }
}
